#!/bin/sh
# git push -u origin pesomama
# git checkout -b pesomama
#npm install -g grunt-node-inspector


# project name & branch
PROJECT=dist
#$1
GIT_ADDRESS=git@172.16.50.222:$PROJECT
BRANCH=dev-pesomama-9


# current su password
SU_PW=Zxc1234567


# remote server conf
SERVER_USER=fda
SERVER_ROOT_USER=root
SERVER_URL=$SERVER_ROOT_USER@139.129.102.169
SERVER_FOLDER=/home/$SERVER_USER/web
SERVER_PW=Zxc1234567
SERVER_SU_PW=$SERVER_PW
SERVER_SH=pct_web-dp.sh

echo '\n\n>>>>>>>>>>>>>>>>>>>> UPDATING ===================='
# update codes
git branch;
git pull;
npm install;


echo '\n\n>>>>>>>>>>>>>>>>>>>> DELETING ===================='
expect -c "
set timeout 900
spawn sudo rm -fr $PROJECT.zip $PROJECT
expect \"*assword\" { send \"$SU_PW\r\" }
expect eof"

expect -c "
spawn ssh -t $SERVER_URL rm -fr $SERVER_FOLDER/*.zip
expect {
\"*assword\" {set timeout 300; send \"$SERVER_PW\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"


echo '\n\n>>>>>>>>>>>>>>>>>>>> BUILDING ===================='
# build dist
expect -c "
set timeout 5000
spawn sudo npm run build
expect \"*assword\" { send \"$SU_PW\r\" }
expect eof"


echo '\n\n>>>>>>>>>>>>>>>>>>>> COMPRESSING ===================='
zip -r $PROJECT.zip $PROJECT


echo '\n\n>>>>>>>>>>>>>>>>>>>> COPYING ===================='
expect -c "
spawn scp -r ./$PROJECT.zip $SERVER_URL:$SERVER_FOLDER
expect {
\"*assword\" {set timeout 900; send \"$SERVER_PW\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"


echo '\n\n>>>>>>>>>>>>>>>>>>>> LOGINING ===================='
expect -c "
set timeout 900
spawn ssh -t $SERVER_URL sudo su
expect \"*assword\" { send \"$SERVER_PW\r\" }
expect \"*assword for $SERVER_USER\" { send \"$SERVER_SU_PW\r\" }
expect \"#\" { send \"cd $SERVER_FOLDER\r\" }
expect \"#\" { send \"sh $SERVER_SH\r\" }
interact"
